    var iG = {};
/* 操作日期
---------------------------------------------------------------------------------------*/
//获取年
iG.getYear = function(date){
    if(date == undefined) date = new Date;
    return date.getFullYear();
};
//获取月
iG.getMonth = function(date){
    if(date == undefined) date = new Date;
    var month = date.getMonth() + 1;
    if(month < 10) month = "0" + month;
    return month;
};
//获取日
iG.getDay = function(date){
   if(date == undefined) date = new Date;
    var day = date.getDate();
    if(day < 10) day = "0" + day;
    return day;
};
//获取当前日期
iG.getDateToday = function(date){
    return iG.getYear() + "-" + iG.getMonth() + "-" + iG.getDay();
};
//获取星期
iG.getWeek = function(date){
    if(date == undefined) date = new Date;
    var week = date.getDay();
    var weekArr = ["日","一","二","三","四","五","六"];
    return weekArr[week];
};
//获取小时
iG.getHours = function(date){
    if(date == undefined) date = new Date;
    return date.getHours();
},
//获取分钟
iG.getMinutes = function(date){
    if(date == undefined) date = new Date;
    return date.getMinutes();
};
    $(function() {

        var ED_D = ST_D = iG.getYear() + "-" + iG.getMonth() + "-" + iG.getDay();
        $('#startDate').text('输入开始日期');
        $('#endDate').text('输入结束日期');
        $("#startDate").on("tap", getStratDate);
        $("#endDate").on("tap", getEndDate);
        //获取开始时间
        function getStratDate() {
            if(! $('#startDate').attr('ST_D')){
                var datese = {
                    year : iG.getYear(),
                    month : iG.getMonth(),
                    day : iG.getDay()
                 }
                $('#startDate').attr('ST_D', JSON.stringify(datese));
            }
            

            var that = $(this);
            var date = JSON.parse($("#startDate").attr("ST_D"));
            uexControl.openDatePicker(date.year, date.month, date.day);
            uexControl.cbOpenDatePicker = function(opCode, dataType, data) {
                var endD = $('#endDate').text();
                //console.log(endD);
                if (dataType == 1) {
                    data = eval('(' + data + ')');

                }
                if (ED_D.split("-")[0] > data.year | (ED_D.split("-")[0] == data.year & ED_D.split("-")[1] > data.month) | (ED_D.split("-")[0] == data.year & ED_D.split("-")[1] == data.month & ED_D.split("-")[2] >= data.day)) {
                        setDate_s(that, data.year, data.month, data.day);
                        ST_D = data.year + '-' + data.month + '-' + data.day;
                      
                }else if(endD === '输入结束日期'){
                        
                    setDate_s(that, data.year, data.month, data.day);
                    ST_D = data.year + '-' + data.month + '-' + data.day;  
                }else{
                    appcan.window.openToast('开始日期不能大于结束日期',3000,5,0);
                    return;
                } 
                    //IC.talkingDataCount("集团客户", "0010");
            }
            setDate_s = function(elem, year, month, day) {
                var month = Number(month);
                var day = Number(day);
                var months,
                    days;
                months = month >= 10 ? "" + month : "0" + month;
                days = day >= 10 ? "" + day : "0" + day;
                var json = {
                    "year" : year,
                    "month" : months,
                    "day" : days,
                };
                $("#startDate").attr("ST_date", year + "-" + months + "-" + days);
                $('#startDate').attr('ST_D', JSON.stringify(json));
                $("#startDate").text(year + "-" + months + "-" + days);
               // console.log(year + "-" + months + "-" + days);
            };

        }

        // 获取结束时间
        function getEndDate() {
            if(!$("#endDate").attr("ED_D")){
                var datase = {
                year : iG.getYear(),
                month : iG.getMonth(),
                day : iG.getDay()
            }
            $("#endDate").attr("ED_D", JSON.stringify(datase));
            }
            
            var that = $(this);
            var date = JSON.parse($("#endDate").attr("ED_D"))
            uexControl.openDatePicker(date.year, date.month, date.day);
            uexControl.cbOpenDatePicker = function(opCode, dataType, data) {
                if (dataType == 1) {
                        data = eval('(' + data + ')');
                }
                if (ST_D.split("-")[0] > data.year | (ST_D.split("-")[0] == data.year & ST_D.split("-")[1] > data.month) | (ST_D.split("-")[0] == data.year & ST_D.split("-")[1] == data.month & ST_D.split("-")[2] > data.day)) {
                        appcan.window.openToast("结束时间不能小于开始时间", 3000, 5, 0);
                        return;
                } else {
                        setDate_s(that, data.year, data.month, data.day);
                        ED_D = data.year + "-" + data.month + "-" + data.day;
                }
                    //IC.talkingDataCount("集团客户", "0010");
            }
            setDate_s = function(elem, year, month, day) {
                var month = Number(month);
                var day = Number(day);
                var months,days;
                months = month >= 10 ? "" + month : "0" + month;
                days = day >= 10 ? "" + day : "0" + day;
                var json = {
                    "year" : year,
                    "month" : months,
                    "day" : days,
                };
                $("#endDate").attr("ED_date", year + "-" + months + "-" + days);
                 $("#endDate").attr("ED_D", JSON.stringify(json));
                $("#endDate").text(year + "-" + months + "-" + days);
            };
        }

    });



    //绑定事件
    $(".search_btn").on("click",function(){
      //获取时间对象

        search();
    });

    function search(){
        IC.talkingDataCount("培训考试","00010");
        appcan.window.openToast("正在加载中",0,5,0);
        var text1=$("#startDate").html();
        var text2=$("#endDate").html();
        if(text1=="输入开始日期" || text2=="输入结束日期"){
            appcan.window.confirm("提示","请输入有效时间范围","确定");
            appcan.window.closeToast();
            return false;
        }
        var userName=appcan.locStorage.getVal("userName"),strStartDate="",strEndDate="";
        var data={"userName":userName,"strStartDate":strStartDate,"strEndDate":strEndDate};
        //console.log(data);
        //var url="http://172.31.64.34:8011/sdk10098/public/yddfMAS/queryUserExams?";

        //var url="http://172.31.65.220:8081/sdk10098/public/yddfMAS/queryUserExams?";
        // var url="http://store.ceair.com/mas/sdk10098/public/yddfMAS/queryUserExams?";

        var url=h_yddfUrl+"yddfMAS/queryUserExams?";
        $.ajax({
            type:"get",
            data:data,
            url:url,
            dataType:"json",
            success:function(data){
                $(".list").html("");//先清空原数据
                var star=Number(($("#startDate").html().replace(/\-/g,"")));
                //alert(star);
                var end=Number(($("#endDate").html().replace(/\-/g,"")));//将表格中的日期 强制转化成数字
                //alert(end);
                appcan.window.closeToast();
                var res=(JSON.parse(data.queryUserExams));
                //console.log(res);
                //console.log(res);
                //请求成功的状态下执行一个方法:
                    if(res.status==0){
                    var examList=res.examList;

                   //遍历考试信息
                    $.each(examList,function(i,value){
                        if(value.startTime!=null){
                            //加载考试详细信息
                            //获取每个开考时间的时间戳 并且判断
                            var times=Number(value.startTime["time"]);//将时间戳强制转化成数字格式
                            var date=new Date(times);
                            var year=date.getFullYear(),M=date.getMonth()+1,D=date.getDate(),H=date.getHours(),m=date.getMinutes(),s=date.getSeconds();
                            //console.log(year);
                            (M<10)?M="0"+M:M=M;//设置月份的格式；
                            (D<10)?D="0"+D:D=D;//设置日期的格式;
                            (H<10)?H="0"+H:H=H;
                            (m<10)?m="0"+m:m=m;
                            (s<10)?s="0"+s:s=s;
                            var time=year+"/"+M+"/"+D;
                           
                        }else{
                            time="暂无数据";
                        }

                        //获取用户创建考试的时间
                        var user_time=Number(value.createTime["time"]);
                        var U_date=new Date(user_time);
                        var U_year=U_date.getFullYear(),U_M=U_date.getMonth()+1,U_D=U_date.getDate();
                        (U_M<10)?U_M="0"+U_M:U_M=U_M;
                        (U_D<10)?U_D="0"+U_D:U_D=U_D;
                        //console.log(U_year);
                        //console.log(times);
                        var U_time=U_year+"/"+U_M+"/"+U_D;
                        var timeSearch=Number(U_year+""+U_M+""+U_D);//中间有bug   有点无语  Bug 还有点奇怪!!

                        //获取最后修改时间
                        //先判断是否存在修改时间
                        if(value.modifyTime !=null){
                            var sav_times=Number(value.modifyTime["time"]);
                            var sav_date=new Date(sav_times);
                            var sav_year=sav_date.getFullYear(),sav_M=sav_date.getMonth()+1,sav_D=sav_date.getDate(),sav_H=sav_date.getHours(),sav_min=sav_date.getMinutes();
                            (sav_M<10)?sav_M="0"+sav_M:sav_M=sav_M;
                            (sav_D<10)?sav_D="0"+sav_D:sav_D=sav_D;
                            (sav_H<10)?sav_H="0"+sav_H:sav_H=sav_H;
                            (sav_min<10)?sav_min="0"+sav_min:sav_min=sav_min;
                            var sav_time=sav_year+"/"+sav_M+"/"+sav_D+" "+sav_H+":"+sav_min;
                            //console.log(sav_time);
                            //alert(sav_time);
                        }else{
                            var sav_time="暂无数据";
                        }

                         var demo;//定义一个变量
                         var pas;
                            if(value.paperSubmit=="SUBMIT"){
                                if(value.score<60){
                                    pas="未及格";
                                    demo=value.score+"("+pas+")";
                                }else{
                                    demo=value.score
                                }
                            }else{
                                demo="暂无数据";
                            }
                            var li=$('<li class="ub-ver ub-pc">'
                                        +'<div class="ub ub-ac kao_title">'
                                            +'<div class="list_l ub ub-ac ub-pj">'
                                                +'<p style="width:70%">'+value.examName+'</p>'
                                                +'<p style="width:30%;text-align:right;color:red">'+demo+'</p>'
                                            +'</div>'
                                        +'</div>'
                                        +'<ul class="detail">'
                                            +"开始时间:"+time+"<br>"
                                            +"创建时间:"+U_time+"<br>"
                                            +"修改时间:"+sav_time
                                            +'<div class="star_exm">进入考试</div>'
                                        +'</ul>'
                                        +'<input type="hidden" value='+value.examId+' id="examId">'
                                        +'<input type="hidden" value='+value.paperId+' id="paperId">'
                                        +'<input type="hidden" value='+value.examTime+' id="examTime">'
                                        +'<input type="hidden" value='+time+' id="startTime">'
                                        +'<input type="hidden" value='+value.paperSubmit+' id="paperSubmit">'
                                  +'</li>');
                        if(timeSearch>=star&&timeSearch<=end){
                            $(".list").append(li);
                        }
                        
                    })
    
                    var avc=$(".list").html();//获取元素中的内容
                    if(avc==""){
                    //当没有数据的时候 给一个提醒;
                    appcan.window.alert("提示","该时间范围暂无数据","确定");
                        return false;
                    }

                    $(".list li").click(function(){
                    IC.talkingDataCount("培训考试","00010");
                       //考试id
                        var examId=$(this).find("#examId").attr("value");
                        //alert(examId);//获取当前元素的考试id
                       //试卷id
                        var paperId=$(this).find("#paperId").attr("value");//获取当前考试 试卷 id
                        //alert(paperId);
                        //考试时长:
                        var examTime=$(this).find("#examTime").attr("value");
                        //alert(examTime);
                        //将考试 id 以及 试卷 id 考试时长保存到本地
                        //先清空之前的本地存储 再 创建新的存储
                        appcan.locStorage.remove("examId");
                        appcan.locStorage.remove("examTime");
                        appcan.locStorage.remove("paperId");
                        appcan.locStorage.setVal("examId",examId);
                        appcan.locStorage.setVal("paperId",paperId);
                        appcan.locStorage.setVal("examTime",examTime);
                        //alert($(this).index());
                        $(this).find("ul").toggleClass("detail_show");
                         //$(this).find("ul").slideToggle(700);
                        $(this).siblings('li').find("ul").removeClass("detail_show");
                        //$(this).siblings('li').slideUp(700);
                        //alert( $(this).find("ul").length);

                       if($(this).find("#paperSubmit").attr("value") == "SUBMIT"){
                        $(this).find(".star_exm").html("已考");
                       };
                    })
                    $(".list li").mouseenter(function(){
                        $(this).css({
                        "background":"#55A67D",
                        "color":"#ffffff"
                        });
                    }).mouseleave(function(){
                           $(this).css({
                            "background":"#ffffff",
                            "color":"#767A8D"
                           });
                    })
                    $(".star_exm").css({
                        "cursor":"pointer",
                        "color":"red",
                        "text-decoration":"underline",
                        "float":"right"
                    });
                    //进入考试详细页面
                    $(".star_exm").on("click",function(event){
                        //alert("进入考试页面");…
                        if($(this).html() == "已考"){
                            appcan.window.alert("提示","已经考过了，不能重复考试","确定");
                            return false;
                            //uexWindow.open("kaosDemo", 0, "kaosDemo.html", 2, "", "", 0);
                        }else{
                            var paperId=$(this).parent("ul").parent().find("#paperId").attr("value");//获取当前考试 试卷 id
                            if(Number(paperId)==0){
                                    appcan.window.alert("提示","没有该考试信息","确定");
                                    return false;
                                }
                             uexWindow.open("kaosDemo", 0, "kaosDemo.html", 2, "", "", 0);  
                        }
                        event.stopPropagation();//阻止冒泡
                    })
                   //请求异常:
                }else{
                    //alert("请求异常");
                }
            },
            error:function(){
               //alert("请求失败");
             }
        })

    }
